package com.itqian.controller.seller;

import com.itqian.common.exception.Assert;
import com.itqian.common.exception.ResponseEnum;
import com.itqian.common.utils.ResultVOUtil;
import com.itqian.common.vo.ResultVO;
import com.itqian.seller.entity.StoreInfo;
import com.itqian.seller.form.StoreIconForm;
import com.itqian.seller.form.StoreInfoForm;
import com.itqian.seller.mapper.StoreInfoMapper;
import com.itqian.seller.service.StoreInfoService;
import com.itqian.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @author shishuqian
 * date 2022/3/21
 * time 20:52
 **/
@RestController
@RequestMapping("/seller/store")
public class StoreInfoController {

    @Autowired
    private StoreInfoService storeInfoService;

    @Autowired
    private StoreInfoMapper storeInfoMapper;

    /**
     *  更新店铺信息
     */
    @PostMapping("/update")
    public ResultVO update(@RequestHeader("token") String token, @Valid @RequestBody StoreInfoForm form){
       //获取店铺id
        Integer storeId = TokenUtils.getStoreId(token);
        //查询该店铺信息
        StoreInfo store = storeInfoService.getById(storeId);
        store.setStoreName(form.getStoreName())
                .setStorePhone(form.getStorePhone())
                .setStoreAddress(form.getStoreAddress())
                .setStoreOpenTime(form.getStoreOpenTime())
                .setStoreNotice(form.getStoreNotice())
                .setStoreType(form.getStoreType())
                .setMinPrice(form.getMinPrice())
                .setPostPrice(form.getPostPrice());
        //更新数据到数据库
        boolean b = storeInfoService.updateById(store);
        if (!b){
            return ResultVOUtil.fail("更新店铺信息失败，请重试...");
        }
        return ResultVOUtil.success(null);
    }

    /**
     *   更改店铺营业状态
     */
    @PutMapping("/status")
    public ResultVO status(@RequestHeader("token") String token){

        Integer storeId = TokenUtils.getStoreId(token);

        StoreInfo store = storeInfoService.getById(storeId);
        store.setStoreStatus(!store.getStoreStatus());
        boolean b = storeInfoService.saveOrUpdate(store);
        if (!b){
            return ResultVOUtil.fail("修改店铺状态失败，请重试...");
        }
        return ResultVOUtil.success(null);
    }

    @GetMapping("/info")
    public ResultVO storeInfo(@RequestHeader("token") String token){
        Integer storeId = TokenUtils.getStoreId(token);
        StoreInfo store = storeInfoService.getById(storeId);
        store.setStoreId(null)
                .setSellerId(null);

        return ResultVOUtil.success(store);

    }

    @PostMapping("updateIcon")
    public ResultVO updateIcon(@RequestHeader("token") String token,@Valid @RequestBody StoreIconForm form){

        Integer storeId = TokenUtils.getStoreId(token);
        boolean b = storeInfoMapper.setIcon(storeId, form.getStoreIcon());
        return ResultVOUtil.success(null);
    }


}
